package com.xmb.wechat.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.PermissionUtils;
import com.nil.sdk.ui.BaseFragmentV4;
import com.xmb.wechat.R;
import com.xmb.wechat.definterface.EmptyDeliver;
import com.xmb.wechat.definterface.HintListener;
import com.xmb.wechat.definterface.PermissionRequestCallBack;
import com.xmb.wechat.dialog.ExemptionDialog;

import java.util.List;
import java.util.Objects;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @Author lijiangop
 * @CreateTime 2019/7/9 11:03
 */
@SuppressLint("ValidFragment")
public abstract class BaseFragment extends BaseFragmentV4 {

    protected boolean         isFirstLoad = false;//是否是第一次加载
    protected LayoutInflater  inflater;
    private   int             res;//资源文件ID
    private   Unbinder        unbinder;
    private   ExemptionDialog mExemptionDialog;

    public BaseFragment(int res) {
        this.res = res;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        View view = inflater.inflate(res, null, false);
        unbinder = ButterKnife.bind(this, view);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(lp);
        initViews(view);
        isFirstLoad = true;
        if (getUserVisibleHint()) {//如果Fragment可见进行数据加载
            onLazyLoad();
            isFirstLoad = false;
        }
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isFirstLoad && isVisibleToUser) {//视图变为可见并且是第一次加载
            onLazyLoad();
            isFirstLoad = false;
        }
    }

    protected void initViews(View view) {
    }

    protected void onLazyLoad() {
    }

    /**
     * 权限请求
     */
    @SuppressLint("WrongConstant")
    protected void doPermissionsRequest(PermissionRequestCallBack callBack, String[] permissions) {
        if (!PermissionUtils.isGranted(permissions)) {
            PermissionUtils.permission(permissions).callback(new PermissionUtils.FullCallback() {

                @Override
                public void onGranted(List<String> permissionsGranted) {
                    callBack.onSuccess();
                }

                @Override
                public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied) {//权限回调规律,全部请求完才进回调
                    if (permissionsDeniedForever.size() > 0) {
                        //do something 权限被永久拒绝了
                    } else
                        doPermissionsRequest(callBack, permissions);
                }
            }).request();
        } else {
            callBack.onSuccess();
        }
    }

    /**
     * 展示免责声明
     */
    protected void showExemption(EmptyDeliver emptyDeliver) {
        //全局配置
        if (!TextUtils.equals(getString(R.string.is_show_exemption), "true")) {
            if (emptyDeliver != null)
                emptyDeliver.onDeliver();
            return;
        }
        if (mExemptionDialog == null) {
            mExemptionDialog = new ExemptionDialog(Objects.requireNonNull(getActivity()), new HintListener() {
                @Override
                public void onConfirm() {
                    if (emptyDeliver != null)
                        emptyDeliver.onDeliver();
                }

                @Override
                public void onCancel() {
                    //doNothing
                }
            });
        }
        mExemptionDialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null){
            unbinder.unbind();
        }
        if (mExemptionDialog != null) {
            mExemptionDialog.dismiss();
            mExemptionDialog = null;
        }
    }
}
